package org.nepu.scr.indexrate.ratelongevity.longevityscore;

import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONUtil;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.jpa.jpa.AbstractJpaCurdExt;
import org.lboot.s3.client.S3Client;
import org.nepu.scr.indexrate.ratelongevity.longevityscore.params.LongevityScoreData;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class LongevityScoreServiceImpl extends AbstractJpaCurdExt<String, LongevityScore, LongevityScoreDAO> implements LongevityScoreService {
    S3Client s3Client;

    public static final String SCHEME_BUCKET_NAME = "longevity";

    @Override
    public List<LongevityScore> getByLongevityName(String longevityId) {
        return dao.findByLongevityName(longevityId);
    }

    @Override
    public LongevityScoreData saveLongevityData(String longevityDataId, LongevityScoreData data) {
        LongevityScore longevity = getOneUnwrap(longevityDataId);

        String longevityId = longevity.getLongevityName();

        String objectName = longevityId + "/" + longevityDataId + "/data.json";

        String jsonStr = JSONUtil.toJsonStr(data);

        s3Client.putObject(SCHEME_BUCKET_NAME,objectName,new ByteArrayInputStream(jsonStr.getBytes(StandardCharsets.UTF_8)));

        return data;
    }

    @Override
    public LongevityScoreData readLongevityData(String longevityId) {
        LongevityScore longevity = getOneUnwrap(longevityId);

        String eId = longevity.getLongevityName();

        String objectName = eId + "/" + longevityId + "/data.json";

        InputStream resultStream = s3Client.getObjectStream(SCHEME_BUCKET_NAME,objectName);
        String result = IoUtil.read(resultStream,StandardCharsets.UTF_8);
        return JSONUtil.toBean(result, LongevityScoreData.class);
    }

    @Override
    public boolean deleteLongevityData(String longevityId) {
        LongevityScore longevity = getOneUnwrap(longevityId);
        if (longevity == null) {
            // 处理未找到SalvageLicence的情况
            return false;
        }
        String wellId = longevity.getLongevityName();
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(SCHEME_BUCKET_NAME) // 这里添加存储桶名称
                .withPrefix(wellId + "/" + longevityId);

        ObjectListing objectListing;
        try {
            objectListing = s3Client.listObjects(listObjectsRequest);
        } catch (AmazonServiceException e) {
            // 处理AmazonServiceException，例如存储桶不存在或其他服务错误
            e.printStackTrace();
            return false;
        }

        List<S3ObjectSummary> sums = objectListing.getObjectSummaries();
        for (S3ObjectSummary s : sums) {
            s3Client.deleteObject(SCHEME_BUCKET_NAME, s.getKey());
        }

        return true;
    }
}
